<?php
/**
* 保险试算控制类
*/
namespace Home\Controller;
class PremiumController extends HomeController {
    public function index(){
        $ModelBrand = M('CarBrand');
        $brands = $ModelBrand->field('brand_id,brand_name,brand_alpha')->select();
        $this->assign('brands',$brands);
        $this->display();
    }
    
    public function getSeries($id=''){
        if(empty($id)){
            $this->error('错误的id');
        }else{
            $model = M('CarSeries');
            $where['brand_id'] = $id;
            $series = $model->where($where)->select();
            $this->success($series);
        }
    }
    
    public function getModel($id=''){
        if(empty($id)){
            $this->error('错误的id');
        }else{
            $model = M('CarModel');
            $where['series_id'] = $id;
            $models = $model->where($where)->select();
            $this->success($models);
        }
    }
    
    public function getDetail($model_id=''){
        if(empty($model_id)){
            $this->error('输入ID错误！');
        }
        
        $model = M('CarModelDetail');
        $where['model_id'] = $model_id;
        $res = $model->where($where)->find();
        if($res){
            $this->success($res);
        }else{
            $this->error('车型库中找不到对应的车型！');
        }
    }
    
    public function select($model){
        $where['model_id'] = $model;
        $model = M('CarModel');
        $resModel = $model->where($where)->find();
        if($resModel){
            $this->assign('model_name',$resModel['model_name']);
        }
        $modelCar = M('CarModelDetail');
        $res = $modelCar->where($where)->find();
        if($res){
            $this->assign('price',$res['vehicle_price']);
            $this->assign('seatnum',$res['limit_load_person']);
            $this->assign('pl',$res['ext_msr']);
        }
        $this->assign('yh',C('FL_YHFL'));
        $this->display();
    }
   
    public function calcSimple($price='',$time='',$days=''){
        if(!IS_POST){
            $this->error('请输入参数');
        }
        if(empty($price)){
            $this->error('价格未填写！');
        }
        if(empty($time)){
            $this->error('购车时间未填写');
        }
        if(empty($days)){
            $this->error('车辆使用频率未填写');
        }
        if($days>7) $days = 7;
        
        $datetime = new \DateTime($time);
        $now = new \DateTime();
        
        $year = $datetime->diff($now)->y;
        
        $seatnum = 5;
        $nIndex = 0; //默认6座以下
        $total = 0;
        //第三者责任险
        $fl_dsz = C('FL_DSZZRX');
        $DiSanZhe = explode('|',$fl_dsz[$nIndex]);
        $total += $DiSanZhe[5]; //选50万的
        
        //车辆损失险
        $fl_clss = C('FL_CLSSX');
        $flArr = explode('||',$fl_clss[$nIndex]);
        if($year < 1 ){
            $params = explode('|',$flArr[0]);
        }else if($year < 2){
            $params = explode('|',$flArr[1]);
        }else if($year < 6){
            $params = explode('|',$flArr[2]);
        }else{
            $params = explode('|',$flArr[3]);
        }
        $SunShi = $params[0] + $price * $params[1] / 100.0;
        
        $total += $SunShi;
        
        //车上人员责任险
        $fl_csry = C('FL_CSRYZRX');
        $flArr = explode('|',$fl_csry[$nIndex]);
        $SiJi = $flArr[0]*10000/100.0; //默认1万
        $ChengKe = $flArr[1]*($seatnum-1)*10000/100.0; //默认1万
        
        $total += $SiJi + $ChengKe;

        //盗抢险
        $fl_dqx = C('FL_DQX');
        $flArr = explode('|',$fl_dqx[$nIndex]);
        $DaoQiang = $flArr[0] + $flArr[1] * $price /100.0;
        $total += $DaoQiang;
        
        /************默认不选*********
        //玻璃单独破碎险
        $fl_bl = C('FL_BLDDPSX');
        $flArr = explode('|',$fl_bl[$nIndex]);
        $BoLi = array($flArr[0] * $price / 100.0, $flArr[1] * $price / 100.0);
        
        //车身划痕险
        $fl_hh = C('FL_CSHHSSX');
        $HuaHen = array();
        foreach($fl_hh as $row){
            $fl_hh_1 = explode('||',$row);
            if($year < 2 ){//2年以下
                $fl_hh_2 = explode('|',$fl_hh_1[0]);
                if($price<300000){
                    $HuaHen[] = $fl_hh_2[0];
                }else if($price<500000){
                    $HuaHen[] = $fl_hh_2[1];
                }else{
                    $HuaHen[] = $fl_hh_2[2];
                }
            }else{
                $fl_hh_2 = explode('|',$fl_hh_1[1]);
                if($price<300000){
                    $HuaHen[] = $fl_hh_2[0];
                }else if($price<500000){
                    $HuaHen[] = $fl_hh_2[1];
                }else{
                    $HuaHen[] = $fl_hh_2[2];
                }
            }                   
        }
        
        //自燃险
        $fl_zrx = C('FL_ZRSSX');
        $ZiRan = $fl_zrx * $price / 100;
        
        //涉水发动机损坏保险
        $fl_ssx = C('FL_SSX');
        $SheShui = $fl_ssx * $price / 100;
        *****************************/
        //不计免赔特约条款
        $MianPei = C('FL_BJMP');
        
        //交强险
        $fl_jqx = C('FL_JQX');
        if($nIndex<2){
            $JiaoQiang = explode('|',$fl_jqx[$nIndex]);
        }else{
            $JiaoQiang = explode('|',$fl_jqx[1]);
        }
        //$total += $JiaoQiang[0]; //一律960
        
        //车船税
        $fl_ccs = C('FL_CCS');
        if($pl<1.0){
            $CheChuan = $fl_ccs[0];
        }else if($pl<1.6){
            $CheChuan = $fl_ccs[1];
        }else if($pl<2.0){
            $CheChuan = $fl_ccs[2];
        }else if($pl<2.5){
            $CheChuan = $fl_ccs[3];
        }else if($pl<3.0){
            $CheChuan = $fl_ccs[4];
        }else if($pl<4.0){
            $CheChuan = $fl_ccs[5];
        }else{
            $CheChuan = $fl_ccs[6];
        }
        
        //$total += 300; //车船税300
        
        //优惠系数
        $YouHui = C('FL_YHFL');
        
        $total1 = round($total*0.85);
        $zt3 = round($total1*0.1);
        $zt4 = round($total*0.85*0.4*((7-$days) / 7));
        $result = array(
            'total' => $total,
            'nt1' => $total,
            'nt2' => $total,
            'tt1' => $total1,
            'tt2' => $total1,
            'zt1' => $total1-$zt3-$zt4,
            'zt2' => $total1,
            'zt3' => $zt3,
            'zt4' => $zt4,
            'zt5' => $zt3+$zt4,
        );
        
        $this->success($result);
    }
        
    /**
    * 获取保险费率参数,供脚本调用
    * 
    * @param mixed $company 保险公司ID，暂时未使用
    */
    public function getConfig($company=''){
        //第三者责任险
        $dsz = array();
        $fl_dsz = C('FL_DSZZRX');
        foreach($fl_dsz as $fl){
            $dsz[] = explode('|',$fl);
        }

        //车辆损失险
        $clss = array();
        $fl_clss = C('FL_CLSSX');
        foreach($fl_clss as $fl){
            $item = array();
            $flArr = explode('||',$fl);
            foreach($flArr as $fl1){
                $item[] = explode('|',$fl1);
            }
            $clss[] = $item;
        }
        
        //车上人员责任险
        $csry = array();
        $fl_csry = C('FL_CSRYZRX');
        foreach($fl_csry as $fl){
            $csry[] = explode('|',$fl);
        }

        //盗抢险
        $dqx = array();
        $fl_dqx = C('FL_DQX');
        foreach($fl_dqx as $fl){
            $dqx[] = explode('|',$fl);
        }
        
        //玻璃单独破碎险
        $bl = array();
        $fl_bl = C('FL_BLDDPSX');
        foreach($fl_bl as $fl){
            $bl[] = explode('|',$fl);
        }       
        
        //车身划痕险
        $cshh = array();
        $fl_hh = C('FL_CSHHSSX');
        foreach($fl_hh as $fl){
            $item = array();
            $flArr = explode('||',$fl);
            foreach($flArr as $fl1){
                $item[] = explode('|',$fl1);
            }
            $cshh[] = $item;
        }
        
        //自燃险
        $zrx = C('FL_ZRSSX');
        
        //涉水发动机损坏保险
        $ssx = C('FL_SSX');

        //不计免赔特约条款
        $bjmp = C('FL_BJMP');

        //交强险
        $jqx = array();
        $fl_jqx = C('FL_JQX');
        foreach($fl_jqx as $fl){
            $jqx[] = explode('|',$fl);
        }
        
        //车船税
        $ccs = C('FL_CCS');
        
        //优惠系数
        $yh = C('FL_YHFL');

        $ret = array(
            'dsz' => $dsz,
            'clss' => $clss,
            'csry' => $csry,
            'dqx' => $dqx,
            'bl' => $bl,
            'cshh' => $cshh,
            'zrx' => $zrx,
            'ssx' => $ssx,
            'bjmp' => $bjmp,
            'jqx' => $jqx,
            'ccs' => $ccs,
            'yh' => $yh,
        );
        $this->success($ret);
    }
    
    protected function calc($price,$seatnum,$year,$pl){      
        if($seatnum<6){
            $nIndex = 0;
        }else if($seatnum>=6 && $seatnum<10){
            $nIndex = 1;
        }else if($seatnum>=10){
            $nIndex = 2;
        }
        
        //第三者责任险
        $fl_dsz = C('FL_DSZZRX');
        $DiSanZhe = explode('|',$fl_dsz[$nIndex]);
        
        //车辆损失险
        $fl_clss = C('FL_CLSSX');
        $flArr = explode('||',$fl_clss[$nIndex]);
        if($year < 1 ){
            $params = explode('|',$flArr[0]);
        }else if($year < 2){
            $params = explode('|',$flArr[1]);
        }else if($year < 6){
            $params = explode('|',$flArr[2]);
        }else{
            $params = explode('|',$flArr[3]);
        }
        $SunShi = $params[0] + $price * $params[1] / 100.0;
        
        //车上人员责任险
        $fl_csry = C('FL_CSRYZRX');
        $flArr = explode('|',$fl_csry[$nIndex]);
        $SiJi = array($flArr[0]*10000/100.0,$flArr[0]*20000/100.0,$flArr[0]*30000/100.0,$flArr[0]*40000/100.0,$flArr[0]*50000/100.0);
        $ChengKe = array($flArr[1]*($seatnum-1)*10000/100.0,$flArr[1]*($seatnum-1)*20000/100.0,$flArr[1]*($seatnum-1)*30000/100.0,$flArr[1]*($seatnum-1)*40000/100.0,$flArr[1]*($seatnum-1)*50000/100.0);

        //盗抢险
        $fl_dqx = C('FL_DQX');
        $flArr = explode('|',$fl_dqx[$nIndex]);
        $DaoQiang = $flArr[0] + $flArr[1] * $price /100.0;
        
        //玻璃单独破碎险
        $fl_bl = C('FL_BLDDPSX');
        $flArr = explode('|',$fl_bl[$nIndex]);
        $BoLi = array($flArr[0] * $price / 100.0, $flArr[1] * $price / 100.0);       
        
        //车身划痕险
        $fl_hh = C('FL_CSHHSSX');
        $HuaHen = array();
        foreach($fl_hh as $row){
            $fl_hh_1 = explode('||',$row);
            if($year < 2 ){//2年以下
                $fl_hh_2 = explode('|',$fl_hh_1[0]);
                if($price<300000){
                    $HuaHen[] = $fl_hh_2[0];
                }else if($price<500000){
                    $HuaHen[] = $fl_hh_2[1];
                }else{
                    $HuaHen[] = $fl_hh_2[2];
                }
            }else{
                $fl_hh_2 = explode('|',$fl_hh_1[1]);
                if($price<300000){
                    $HuaHen[] = $fl_hh_2[0];
                }else if($price<500000){
                    $HuaHen[] = $fl_hh_2[1];
                }else{
                    $HuaHen[] = $fl_hh_2[2];
                }
            }                   
        }
        
        //自燃险
        $fl_zrx = C('FL_ZRSSX');
        $ZiRan = $fl_zrx * $price / 100;
        
        //涉水发动机损坏保险
        $fl_ssx = C('FL_SSX');
        $SheShui = $fl_ssx * $price / 100;
        
        //不计免赔特约条款
        $MianPei = C('FL_BJMP');
        
        //交强险
        $fl_jqx = C('FL_JQX');
        if($nIndex<2){
            $JiaoQiang = explode('|',$fl_jqx[$nIndex]);
        }else{
            $JiaoQiang = explode('|',$fl_jqx[1]);
        }
        
        //车船税
        $fl_ccs = C('FL_CCS');
        if($pl<1.0){
            $CheChuan = $fl_ccs[0];
        }else if($pl<1.6){
            $CheChuan = $fl_ccs[1];
        }else if($pl<2.0){
            $CheChuan = $fl_ccs[2];
        }else if($pl<2.5){
            $CheChuan = $fl_ccs[3];
        }else if($pl<3.0){
            $CheChuan = $fl_ccs[4];
        }else if($pl<4.0){
            $CheChuan = $fl_ccs[5];
        }else{
            $CheChuan = $fl_ccs[6];
        }
        
        //优惠系数
        $YouHui = C('FL_YHFL');
       
        $result = array(
            'DiSanZhe' => $DiSanZhe,
            'SunShi' => $SunShi,
            'SiJi' => $SiJi,
            'ChengKe' => $ChengKe,
            'DaoQiang' => $DaoQiang,
            'BoLi' => $BoLi,
            'HuaHen' => $HuaHen,
            'ZiRan' => $ZiRan,
            'SheShui' => $SheShui,
            'MianPei' => $MianPei,
            'JiaoQiang' => $JiaoQiang,
            'CheChuan' => $CheChuan,
            'YouHui' => $YouHui,
        );
        return $result;
    }
    
    
    /**
    * 第三者责任险计算
    * 
    */
    public function dszzrx(){
        $retVal = false;
        $fl = C('FL_DSZZRX');
        $seatnum = I('n_seatnum'); //座位数
        $amt = I('n_amt');  //投保金额
        $nIndex = 0; //默认6座以下
        if($seatnum>=6 && $seatnum<=10){
            $nIndex = 1;
        }else if($seatnum>10){
            $nIndex = 2;
        }
        $flArr = explode(',',$fl[$nIndex]);
        if($amt == 50000){
            $retVal = $flArr[0];
        }else if($amt == 100000){
            $retVal = $flArr[0];
        }else if($amt == 150000){
            $retVal = $flArr[0];
        }else if($amt == 200000){
            $retVal = $flArr[0];
        }else if($amt == 300000){
            $retVal = $flArr[0];
        }else if($amt == 500000){
            $retVal = $flArr[0];
        }else if($amt == 1000000){
            $retVal = $flArr[0];
        }
        if($retVal === false){
            $this->error('无效的保险额');
        }else{
            $this->success($retVal);
        }
    }
    
    /**
    * 车辆损失险
    * 
    */
    public function clssx(){
        $fl = C('FL_CLSSX');
        $seatnum = I('n_seatnum');  //座位数
        $amt = I('n_amt'); //投保车辆裸车价格
        $year = I('n_year'); //车辆年限
        $nIndex = 0; //默认6座以下
        if($seatnum>=6 && $seatnum<=10){
            $nIndex = 1;
        }else if($seatnum>10){
            $nIndex = 2;
        }
        $flArr = explode(';',$fl[$nIndex]);
        if($year < 1 ){
            $params = explode(',',$flArr[0]);
        }else if($year < 2){
            $params = explode(',',$flArr[1]);
        }else if($year <= 6){
            $params = explode(',',$flArr[2]);
        }else if($year > 6){
            $params = explode(',',$flArr[3]);
        }
        if($param){
            $retVal = $params[0] + $amt * $params[1] / 100.0;
            $this->success($retVal);
        }else{
            $this->error('无效的年份选择');    
        }
    }
    
    /**
    * 车上人员责任险
    * 
    */
    public function csryzrx(){
        $fl = C('FL_CSRYZRX');
        $seatnum = I('n_seatnum'); //座位数
        $amt = I('n_amt');//投保金额
        $nIndex = 0; //默认6座以下
        if($seatnum>=6 && $seatnum<=10){
            $nIndex = 1;
        }else if($seatnum>10){
            $nIndex = 2;
        }
        $flArr = explode(',',$fl[$nIndex]);
        $retVal = $flArr[0]*$amt/100.0 + $amt * $flArr[1]*($seatnum-1)/100.0;
        $this->success($retVal);
    }

    /**
    * 盗抢险
    *    
    */
    public function dqx(){
        $fl = C('FL_DQX');
        $seatnum = I('n_seatnum');   //座位数
        $amt = I('n_amt'); //裸车价格
        $nIndex = 0; //默认6座以下
        if($seatnum>=6 && $seatnum<=10){
            $nIndex = 1;
        }else if($seatnum>10){
            $nIndex = 2;
        }
        $flArr = explode(',',$fl[$nIndex]);
        $retVal = $flArr[0]+$flArr[1]*$amt/100.0;
        $this->success($retVal);
    }
    
    /**
    * 玻璃单独破碎险
    * 
    */
    public function blx(){
        $fl = C('FL_BLDDPSX');
        $seatnum = I('n_seatnum');   //座位数
        $amt = I('n_amt'); //裸车价格
        $bltype = I('n_type'); //是否国产玻璃
        $nIndex = 0; //默认6座以下
        if($seatnum>=6 && $seatnum<=10){
            $nIndex = 1;
        }else if($seatnum>10){
            $nIndex = 2;
        }
        $flArr = explode(',',$fl[$nIndex]);
        if($bltype == 1){
            $retVal = $flArr[0]*$amt/100.0;
        }else{
            $retVal = $flArr[1]*$amt/100.0;
        }
        $this->success($retVal);
    }
    
    /**
    * 车身划痕损失险
    * 
    */
    public function cshhssx(){
        $fl = C('FL_CSHHSSX');
        $amt = I('n_amt'); //裸车价格
        $amt2= I('n_amt2');//投保金额
        $year = I('n_year'); //车辆年限
        $nIndex = 0; //默认2000
        if($amt2 == 5000){
            $nIndex = 1;
        }else if($amt2 == 10000){
            $nIndex = 2;
        }else if($amt2 == 20000){
            $nIndex = 3;
        }
        $flArr = explode(';',$fl[$nIndex]);
        if($year < 2 ){//2年以下
            $params = explode(',',$flArr[0]);
        }else{
            $params = explode(',',$flArr[1]);
        }
        if($param){
            $retVal = $params[0] + $amt * $params[1] / 100.0;
            $this->success($retVal);
        }else{
            $this->error('无效的年份选择');    
        }
    }
}
  
